package com.android.launcher3;

import android.app.Application;
import android.content.ContentResolver;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.ContentObserver;
import android.os.Handler;

/**
 * 完整
 * 
 */
public class LauncherApplication extends Application {
	public IconCache mIconCache;
	public LauncherModel mModel;

	/** Receives notifications whenever the user favorites have changed. */
	private final ContentObserver mFavoritesObserver = new ContentObserver(
			new Handler()) {
		@Override
		public void onChange(final boolean selfChange) {
			mModel.startLoader(LauncherApplication.this, false);
		}
	};

	protected static final String TAG = "LauncherApplication";

	@Override
	public void onCreate() {
		// VMRuntime.getRuntime().setMinimumHeapSize(4 * 1024 * 1024);
		super.onCreate();
		mIconCache = new IconCache(this);
		mModel = new LauncherModel(this, mIconCache);

		// Register intent receivers
		IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
		filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
		filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
		filter.addDataScheme("package");
		registerReceiver(mModel, filter);

		// Register for changes to the favorites
		ContentResolver resolver = getContentResolver();
		resolver.registerContentObserver(
				LauncherSettings.Favorites.CONTENT_URI, true,
				mFavoritesObserver);
	}

	public void onTerminate() {
		super.onTerminate();
		unregisterReceiver(mModel);

		ContentResolver resolver = getContentResolver();
		resolver.unregisterContentObserver(mFavoritesObserver);
	}

	IconCache getIconCache() {
		return this.mIconCache;
	}

	LauncherModel setLauncher(final Launcher launcher) {
		this.mModel.initialize(launcher);

		return this.mModel;
	}
}
